package club.hicode.daydayup.lambda;

import cn.hutool.core.collection.CollectionUtil;
import com.github.sd4324530.jtuple.Tuple2;
import org.junit.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Map 的 lambda 测试
 *
 * @author Liu Chunfu
 * @date 2018-05-14 下午2:59
 **/
public class MapLambda {
    @Test
    public void mapTest() {
        Map<String, List<String>> map = new HashMap<>();

        map.put("a", CollectionUtil.newArrayList("1", "2", "3"));
        map.put("b", CollectionUtil.newArrayList("4", "2"));
        map.put("c", CollectionUtil.newArrayList("6"));

        //todo 上线和下线的业务

        //Stream<Stream<Tuple2<String, String>>> streamStream = map.keySet()
        //        .stream().flatMap(k -> map.values().stream().flatMap(Collection::stream).map(v -> Tuple2.with(k, v)));

        List<String[]> collect = map.keySet().stream().flatMap(k -> map.values().stream().flatMap(Collection::stream).map(v -> new String[]{v, k})).collect(Collectors.toList());
        for (String[] strings : collect) {
            System.out.println(strings[0] + ":" + strings[1]);
        }
    }

    @Test
    public void test2() {
        Map<String, List<String>> map = new HashMap<>();
        map.put("a", CollectionUtil.newArrayList("1", "2", "3"));
        map.put("b", CollectionUtil.newArrayList("4", "2"));
        map.put("c", CollectionUtil.newArrayList("6"));

        List<Tuple2<String, String>> collect = map.keySet().stream().flatMap(k -> map.values().stream().flatMap(Collection::stream).map(v -> Tuple2.with(v, k))).collect(Collectors.toList());
        for (Tuple2<String, String> tuple2 : collect) {
            System.out.println(tuple2.first + ":" + tuple2.second);
        }
    }

    //仍然是引用类型
    @Test
    public void test1() {
        Person person = new Person("i1", "xxx", "xxx");
        List<Person> personList = CollectionUtil.newArrayList(person);

        Optional<Person> first = personList.stream().findFirst();
        first.ifPresent(p -> p.setId("33333"));
        System.out.println(person);
    }

    @Test
    public void test222() {
        Person person = new Person("i1", "xxx", "xxx");
        List<Person> personList = CollectionUtil.newArrayList(person);

        personList = personList.stream().map(p -> {
            p.setId("xxx");
            return p;
        }).collect(Collectors.toList());

        for (Person person1 : personList) {
            System.out.println(person1);
        }
    }
}
